package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/handleComputer")
public class HandleComputer extends HttpServlet{
    private static final long serialVersionUID=1L;
    protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        request.setCharacterEncoding("utf-8");
        int first=Integer.parseInt(request.getParameter("firstNumber"));
        int second=Integer.parseInt(request.getParameter("secondNumber"));
        String ope=request.getParameter("operator");
        Computer computer=new Computer();
        computer.setFirstNumber(first);
        computer.setSecondNumber(second);
        computer.setOperator(ope);
        CalculateBean calculateBean=new CalculateBean();
        if(calculateBean.calculate(computer)){
            request.setAttribute("computer",computer);
            RequestDispatcher dis=request.getRequestDispatcher("showResult.jsp");
            dis.forward(request,response);
        }else{
            response.sendRedirect("inputNumber.jsp");
        }
    }
}
